/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUIRegistrarMovimiento.java
 *
 * Created on Oct 20, 2011, 2:26:15 AM
 */
package GUI.Procesamiento;

import BE.BEProducto;
import GUI.Pallet.GUIAsignarPallets;
import BE.BEContacto;
import BE.BELineaDespacho;
import BE.BELineaMovimiento;
import BE.BEMovimiento;
import BE.BEPallet;
import BE.BEProductoxTipoPallet;
import BL.BLCombo;
import BL.BLPallet;
import BL.BLProducto;
import BL.BLValidaciones;
import BL.BLmovimiento;
import DA.DAContacto;
import GUI.Contactos.GUIMantenimientoContacto;
import GUI.Paleta.GUIMantenimientoPaleta;
import GUI.Pallet.GUIMantenimientoPallet;
import GUI.Productos.GUIMantenimientoProducto;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author fenartcos
 */
public class GUIRegistrarDespacho extends javax.swing.JDialog {
    private BEMovimiento objMovimiento;
    private BEContacto objcontacto;
    private List<BELineaMovimiento> lineas=new ArrayList<BELineaMovimiento>();
    private BEPallet objpallet = new BEPallet();
    private BEProducto objproducto;

    private List<BELineaDespacho> listapedido = new ArrayList<BELineaDespacho>();
    /** Creates novew form GUIRegistrarMovimiento */
    public GUIRegistrarDespacho(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
         this.setLocationRelativeTo(null);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int height = screenSize.height;
        int width = screenSize.width;
        this.setLocation(width/2-this.getWidth()/2, height/2-this.getHeight()/2);
       objMovimiento= new BEMovimiento();
        java.util.Date fecha = new Date();

        jDCfecha.setDate(fecha);

       txtNroCajas.addKeyListener(new BLValidaciones().soloNumeros);

        BLCombo blCombo = new BLCombo();
        //Formatos para componentes
        blCombo.setCombo("TIPOPALETA",1,cmbTipo);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        pnlMovimiento = new javax.swing.JPanel();
        lblFecha = new javax.swing.JLabel();
        txtCliProv = new javax.swing.JTextField();
        lblCliProv = new javax.swing.JLabel();
        jDCfecha = new com.toedter.calendar.JDateChooser();
        btnBusqueda = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtProducto = new javax.swing.JTextField();
        btnProducto = new javax.swing.JButton();
        cmbTipo = new javax.swing.JComboBox();
        lblTipoPallet = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtCapacidad = new javax.swing.JTextField();
        txtNroCajas = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblDespacho = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        btnAceptar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Registrar Despacho");

        pnlMovimiento.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Generales"));

        lblFecha.setText("Fecha:");

        txtCliProv.setEnabled(false);
        txtCliProv.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCliProvActionPerformed(evt);
            }
        });

        lblCliProv.setText("Cliente:");

        jDCfecha.setEnabled(false);

        btnBusqueda.setText("...");
        btnBusqueda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBusquedaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlMovimientoLayout = new javax.swing.GroupLayout(pnlMovimiento);
        pnlMovimiento.setLayout(pnlMovimientoLayout);
        pnlMovimientoLayout.setHorizontalGroup(
            pnlMovimientoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlMovimientoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlMovimientoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblCliProv)
                    .addComponent(lblFecha))
                .addGap(31, 31, 31)
                .addGroup(pnlMovimientoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(pnlMovimientoLayout.createSequentialGroup()
                        .addComponent(txtCliProv)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnBusqueda, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jDCfecha, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(225, Short.MAX_VALUE))
        );
        pnlMovimientoLayout.setVerticalGroup(
            pnlMovimientoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlMovimientoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlMovimientoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCliProv, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblCliProv)
                    .addComponent(btnBusqueda))
                .addGap(9, 9, 9)
                .addGroup(pnlMovimientoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblFecha)
                    .addComponent(jDCfecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Producto"));

        jLabel1.setText("Producto:");

        txtProducto.setEditable(false);
        txtProducto.setEnabled(false);

        btnProducto.setText("...");
        btnProducto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnProductoActionPerformed(evt);
            }
        });

        cmbTipo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbTipoActionPerformed(evt);
            }
        });

        lblTipoPallet.setText("Tipo de Paleta:");

        jLabel2.setText("Capacidad:");

        txtCapacidad.setEnabled(false);

        txtNroCajas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNroCajasActionPerformed(evt);
            }
        });

        jLabel3.setText("Nro. Cajas:");

        jButton1.setText("Agregar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblTipoPallet)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(txtProducto, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(cmbTipo, 0, 110, Short.MAX_VALUE))
                .addGap(31, 31, 31)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(25, 25, 25)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtNroCajas)
                    .addComponent(txtCapacidad, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE))
                .addGap(248, 248, 248))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3)
                        .addComponent(txtNroCajas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblTipoPallet)
                    .addComponent(jLabel2)
                    .addComponent(txtCapacidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
                .addComponent(jButton1))
        );

        tblDespacho.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Producto", "Nro.Pallets", "Cantidad Total", "TipoPallet"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblDespacho.getTableHeader().setReorderingAllowed(false);
        jScrollPane2.setViewportView(tblDespacho);
        tblDespacho.getColumnModel().getColumn(1).setResizable(false);
        tblDespacho.getColumnModel().getColumn(1).setPreferredWidth(30);
        tblDespacho.getColumnModel().getColumn(2).setResizable(false);
        tblDespacho.getColumnModel().getColumn(2).setPreferredWidth(30);

        btnAceptar.setText("Siguiente");
        btnAceptar.setMaximumSize(new java.awt.Dimension(100, 25));
        btnAceptar.setMinimumSize(new java.awt.Dimension(100, 25));
        btnAceptar.setPreferredSize(new java.awt.Dimension(100, 25));
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.setMaximumSize(new java.awt.Dimension(100, 25));
        btnCancelar.setMinimumSize(new java.awt.Dimension(100, 25));
        btnCancelar.setPreferredSize(new java.awt.Dimension(100, 25));
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        jButton2.setText("Remover");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(26, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addGap(18, 18, 18)
                .addComponent(btnAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnAceptar, btnCancelar, jButton2});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(19, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btnAceptar, btnCancelar, jButton2});

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnlMovimiento, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlMovimiento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void txtCliProvActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCliProvActionPerformed

    }//GEN-LAST:event_txtCliProvActionPerformed

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed

        if (txtCliProv.getText().length()==0)
        { JOptionPane.showMessageDialog(null,"Debe seleccionar un Cliente",
                                          "Informacion",JOptionPane.INFORMATION_MESSAGE);
          return;
        }

        if (txtProducto.getText().length()==0)
        {
            JOptionPane.showMessageDialog(null, "Debe seleccionar un producto",
                                          "Informacion",JOptionPane.INFORMATION_MESSAGE);
        return;
        }


        GUIPlanDeMovilizacionDespacho frmGUIPlan = new GUIPlanDeMovilizacionDespacho(null, true,objcontacto,listapedido);
        this.setVisible(false);
        frmGUIPlan.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_btnAceptarActionPerformed

  



    private void btnBusquedaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBusquedaActionPerformed
       GUIMantenimientoContacto frmBuscarContacto= new GUIMantenimientoContacto (null,true,objcontacto,1);
        frmBuscarContacto.setVisible(true);
        objcontacto= frmBuscarContacto.getObjEmpleado();
        //System.out.println("+++++++++"+objcontacto.getNombre()+"+++++++++++");
        BLPallet blpallet = new BLPallet();

        
        if (objcontacto!=null){

        txtCliProv.setText(objcontacto.getNombre());
          }
 else


        frmBuscarContacto.dispose();

    }//GEN-LAST:event_btnBusquedaActionPerformed

    private void btnProductoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProductoActionPerformed
        // TODO add your handling code here:

        GUIMantenimientoProducto frmBuscarProducto= new GUIMantenimientoProducto(null,true,objproducto);
        frmBuscarProducto.setVisible(true);

        objproducto = new BEProducto();
        objproducto= frmBuscarProducto.getObjProducto();

        if (objproducto!=null)
            txtProducto.setText(objproducto.getNombre());
        frmBuscarProducto.dispose();

    }//GEN-LAST:event_btnProductoActionPerformed

    private void cmbTipoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbTipoActionPerformed
       BLPallet blpallet=new BLPallet();
       int n=0;
       int m=0;
       BLCombo idTipo = (BLCombo)cmbTipo.getSelectedItem();
       
       
       if (txtProducto.getText().length()!=0 && idTipo.id!=0)
       { n=blpallet.getCapacidad(objproducto.getIdProducto(),idTipo.id);
           if (n>0) 
               txtCapacidad.setText(n+"");}
           
        if (txtProducto.getText().length()!=0 && idTipo.id!=0 && txtNroCajas.getText().length()>0){
             
            
           m= Integer.parseInt(txtNroCajas.getText());
           
           
           
            
        if (m % n !=0)
        {
        JOptionPane.showMessageDialog(null,"La cantidad de cajas no es multiplo de: "+n,
                                      "Informacion",JOptionPane.INFORMATION_MESSAGE);
        
        txtNroCajas.setText("");
        
        cmbTipo.setSelectedIndex(0);
        txtCapacidad.setText("");
        }
        }
       

    }//GEN-LAST:event_cmbTipoActionPerformed

    private void txtNroCajasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNroCajasActionPerformed
        // TODO add your handling code here:
       BLPallet blpallet=new BLPallet();
       int n=0;
       int m=0;
       BLCombo idTipo = (BLCombo)cmbTipo.getSelectedItem();
        if (txtProducto.getText().length()!=0 && idTipo.id!=0 && txtNroCajas.getText().length()>0){
             
            n=blpallet.getCapacidad(objproducto.getIdProducto(),idTipo.id);
           if (n>0) 
               txtCapacidad.setText(n+"");
           m= Integer.parseInt(txtNroCajas.getText());
           
           
           
            
        if (m % n !=0)
        {
        JOptionPane.showMessageDialog(null,"La cantidad de cajas no es multiplo de: "+n,
                                      "Informacion",JOptionPane.INFORMATION_MESSAGE);
        
        txtNroCajas.setText("");
        
        cmbTipo.setSelectedIndex(0);
        txtCapacidad.setText("");
        }
        }
       
        
    }//GEN-LAST:event_txtNroCajasActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        
        BLCombo idTipo = (BLCombo)cmbTipo.getSelectedItem();
        
        BLProducto blprod = new BLProducto();
        int m=0;

        if (Integer.parseInt(txtNroCajas.getText())==0 || txtNroCajas.getText().length()==0 )
            JOptionPane.showMessageDialog(null,"Debe ingresar una cantidad",
                                      "Informacion",JOptionPane.INFORMATION_MESSAGE);
        
   int cant=0;
   
   BLPallet blpallet=new BLPallet();
   
   cant=blpallet.getNroPallet(objproducto.getIdProducto(), idTipo.id);//nro pallets actual por prod-paleta
  
   
   
        
        if (txtProducto.getText().length()>0&&txtCapacidad.getText().length()>0&& idTipo.id!=0){
            m=busca(objproducto.getIdProducto(),listapedido);//devuelve la cantidad por producto-paleta en la lista
            int stock=blprod.getCantidad(objproducto.getIdProducto()); //stock actual
           stock=stock-m;
            
           
           
           int cantidadPalletsAgregsdos=obtenerCantidadPallets(objproducto,idTipo.id,listapedido);
          stock=stock-(cantidadPalletsAgregsdos*(Integer.parseInt(txtCapacidad.getText())));
           
           
           if (stock<Integer.parseInt(txtNroCajas.getText()))
            {JOptionPane.showMessageDialog(null,"No hay stock suficiente",
                                      "Informacion",JOptionPane.INFORMATION_MESSAGE);
            return;
            }
             int npalletspedidos=(Integer.parseInt(txtNroCajas.getText()))/(Integer.parseInt(txtCapacidad.getText()));
        
             if (cant<npalletspedidos+cantidadPalletsAgregsdos){
                 JOptionPane.showMessageDialog(null,"No hay suficientes pallets",
                                      "Informacion",JOptionPane.INFORMATION_MESSAGE);
            return;
             }
            
            
            
 BEProductoxTipoPallet objPXT= new BEProductoxTipoPallet(objproducto.getIdProducto(),idTipo.id, Integer.parseInt(txtNroCajas.getText()));
            BELineaDespacho objlineadespacho= new BELineaDespacho(objPXT,npalletspedidos);

           objlineadespacho.getObjPXT().setNombreTipoPallet(cmbTipo.getSelectedItem().toString());
           
           objlineadespacho.getObjPXT().setNombreProd(objproducto.getNombre());
           
          if(!buscaproducto(objlineadespacho,listapedido))
           
           listapedido.add(objlineadespacho);
        
        llenarTabla(listapedido);
            //BLProducto blprod=new BLProducto();
            //int n=blprod.getCantidad(objproducto.getIdProducto());
        }
           else {
               JOptionPane.showMessageDialog(null,"Falta completar datos",
                                      "Informacion",JOptionPane.INFORMATION_MESSAGE);
               return;
           }

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
       int fila = tblDespacho.getSelectedRow();
       if (listapedido.size()>0&& tblDespacho.getSelectedRowCount()>0){
       listapedido.remove(fila);
       llenarTabla(listapedido);}
    }//GEN-LAST:event_jButton2ActionPerformed




     public void llenarTabla(List<BELineaDespacho> lista){


   DefaultTableModel modelo=(DefaultTableModel) tblDespacho.getModel();
for (int i=modelo.getRowCount()-1;i>=0;i--){
    modelo.removeRow(i);
}

if (lista!=null){
    for (int i = 0; i < lista.size(); i++) {
        modelo.addRow(new Object[4]);
        tblDespacho.setValueAt(lista.get(i).getObjPXT().getNombreProd(), i, 0);
        
        
        
        tblDespacho.setValueAt(lista.get(i).getCantidadPallets(), i, 1);

        tblDespacho.setValueAt(lista.get(i).getObjPXT().getCantidad(), i, 2);

        tblDespacho.setValueAt(lista.get(i).getObjPXT().getNombreTipoPallet(), i, 3);



        //tblDespacho.setValueAt(lista.get(i).getCantidadPallets()*lista.get(i).getObjPXT().getCantidad(), i, 3);

    }
    }
}







    /**
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GUIRegistrarDespacho.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GUIRegistrarDespacho.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GUIRegistrarDespacho.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GUIRegistrarDespacho.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                GUIRegistrarDespacho dialog = new GUIRegistrarDespacho(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnBusqueda;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnProducto;
    private javax.swing.JComboBox cmbTipo;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private com.toedter.calendar.JDateChooser jDCfecha;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel lblCliProv;
    private javax.swing.JLabel lblFecha;
    private javax.swing.JLabel lblTipoPallet;
    private javax.swing.JPanel pnlMovimiento;
    private javax.swing.JTable tblDespacho;
    private javax.swing.JTextField txtCapacidad;
    private javax.swing.JTextField txtCliProv;
    private javax.swing.JTextField txtNroCajas;
    private javax.swing.JTextField txtProducto;
    // End of variables declaration//GEN-END:variables

    private int buscaExistencia(int codprod, int tipopaleta) {
       int n=0;
        if (listapedido.size()>0 && listapedido!=null){
            for (int i = 0; i < listapedido.size(); i++) {
                if (codprod==listapedido.get(i).getObjPXT().getIdProducto() && listapedido.get(i).getObjPXT().getIdTipoPallet()==tipopaleta)
                    n=n+listapedido.get(i).getCantidadPallets();

            }


            return n;
        }



            else return 0;
    }

    private int busca(int idProducto, List<BELineaDespacho> listapedido) {
        int n=0;
        
        for (int i = 0; i < listapedido.size(); i++) {
            BELineaDespacho bELineaDespacho = listapedido.get(i);
            if(bELineaDespacho.getObjPXT().getIdProducto()==idProducto)
                n=n+bELineaDespacho.getObjPXT().getCantidad();
            
        }
       return n; 
        
    }

    private boolean buscaproducto(BELineaDespacho objlineadespacho, List<BELineaDespacho> listapedido) {
        boolean resp=false;
        
        for (int i = 0; i < listapedido.size(); i++) {
            BELineaDespacho bELineaDespacho = listapedido.get(i);
            if(bELineaDespacho.getObjPXT().getIdProducto()==objlineadespacho.getObjPXT().getIdProducto() && bELineaDespacho.getObjPXT().getIdTipoPallet()==objlineadespacho.getObjPXT().getIdTipoPallet()){
        
                resp=true;
                bELineaDespacho.setCantidadPallets(objlineadespacho.getCantidadPallets()+bELineaDespacho.getCantidadPallets());
                 bELineaDespacho.getObjPXT().setCantidad(objlineadespacho.getObjPXT().getCantidad()+bELineaDespacho.getObjPXT().getCantidad());
                break;
            }
        }
        
        
        return resp;
    }

    private int obtenerCantidadPallets(BEProducto objproducto,int tipopaleta, List<BELineaDespacho> listapedido) {
      int n=0;
        
        for (int i = 0; i < listapedido.size(); i++) {
            BELineaDespacho bELineaDespacho = listapedido.get(i);
            if(bELineaDespacho.getObjPXT().getIdProducto()== objproducto.getIdProducto() && bELineaDespacho.getObjPXT().getIdTipoPallet()== tipopaleta)
                n=n+bELineaDespacho.getCantidadPallets();
                
        }
        
        
        return n;
    }
}
